package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

/**
 * Created by Administrator on 2016.6.19.
 */
public class Test16_09 extends JFrame{

    public Test16_09(){
        setLayout(new BorderLayout());
        add(new MovableMessagePanel("Welcome to Java"));
    }

    public static void main(String[] args){
        Test16_09 frame=new Test16_09();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Just a test");
        frame.pack();
        frame.setVisible(true);
    }

    static class MovableMessagePanel extends JPanel{
        private String message="Welcome to Java";
        private int x=20;
        private int y=20;

        public MovableMessagePanel(String s){
            message=s;
            addMouseMotionListener(new MouseMotionAdapter(){
                @Override
                public void mouseMoved(MouseEvent e) {
                    super.mouseMoved(e);
                    x=e.getX();
                    y=e.getY();
                    repaint();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(message,x,y);
        }
    }
}
